package com.syh.jn.scm.main;

import cn.redscarf.commons.spring.SpringContextUtils;
import com.syh.jn.scm.service.api.operation.system.OperationAccountService;
import com.syh.jn.scm.service.impl.operation.system.OperationAccountServiceImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author huangzhenxian
 */
public class Startup {
  private static volatile boolean running = true;

  public static void main(String[] args) {
    try {
      ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"context.xml"});

      context.start();
      //addShutdownHook:作用是在jvm做为一个关闭的钩子,当程序正常退出,或系统调用system.exit()或虚拟机关闭
      //才会执行添加在addShutdownHook线程的方法,所以可通过这些钩子在jvm关闭的时候进行内存清理、资源回收等工作
      Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
          synchronized (Startup.class) {
            running = false;
            Startup.class.notify();
          }
        }
      });
      InitEventBus.initReflexEventBus();
      OperationAccountService operationAccountService = (OperationAccountService) SpringContextUtils.getBean(OperationAccountServiceImpl.class);
      operationAccountService.initOperationSystemAccount();

    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
    synchronized (Startup.class) {
      while (running) {
        try {
          System.out.println("running............时间为:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
          Startup.class.wait();
          System.out.println("shutdown...........时间为:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        } catch (Throwable e) {
        }
      }
    }
  }

}
